<?php

class NewsController extends Controller
{
    public $breadcrumbs;
    public $pages;
    public function actionIndex()
    {
		$criteria = new CDbCriteria();
		$criteria->select = 'id, name, description, image, last_update';
		$criteria->condition = 'status=:status AND type=:type';
		$criteria->params = array(':status'=>1, ':type'=>0);

		$count= Advertising::model()->count($criteria);
		$this->pages= new CPagination($count);
		$this->pages->pageSize = 9;
		$this->pages->applyLimit($criteria);

		$ads = Advertising::model()->findAll($criteria);
		
		$this->breadcrumbs = array( array('name' => Yii::t("view_partials_header","News"), 'url' => $this->createUrl('news/index')));

		$this->layout = "PageLayout";
		
		$this->render("index", array("ads" => $ads));
    }

    public function actionDetail($id='')
    {
		if($id){
			$criteria = new CDbCriteria();
			$criteria->select = 'id, name, description, content_vn, content_en, last_update';
			$criteria->condition = 'status=:status AND (type=0 OR type=1) AND id=:id';
			$criteria->params = array(':status'=> 1, ':id' => $id);

			$ads = Advertising::model()->find($criteria);
			if($ads === null)
				throw new CHttpException(404,'The requested page does not exist.');

			$this->breadcrumbs = array( array('name' => Yii::t("view_partials_header","News"), 'url' => $this->createUrl('news/index')),
                             			array('name' => $ads->name, 'url' => $this->createUrl('news/detail', array('id' => $ads->id))),);

			$this->layout = "PageLayout";
			$this->render("Detail", array("ads" => $ads));
		} else {

			$this->breadcrumbs = array( array('name' => Yii::t("view_partials_header","News"), 'url' => $this->createUrl('news/index')),);
			$this->layout = "PageLayout";
			$this->render("Detail", array("ads" => array()));
		}

    }
}